package com.sunagy.demand.backend.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.sunagy.demand.backend.VO.normal.VerifyCodeVO;
import com.sunagy.demand.backend.cache.CacheClient;
import com.sunagy.demand.backend.config.MazConfig;
import com.sunagy.demand.backend.exception.IncorrectSmsCodeException;
import com.sunagy.demand.backend.exception.SmsSendTooFastException;
import com.sunagy.demand.backend.service.SmsService;
import com.sunagy.demand.backend.thirdparty.SmsCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/27
 * time：下午2:05
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private MazConfig mazConfig;

    @Autowired
    private SmsCoreService smsCoreService;

    @Autowired
    private CacheClient cacheClient;
    @Override
    public SendSmsResponse send(String phoneNumber, String tplNumber, HashMap<String,String> attributes) {
        SendSmsResponse sendSmsResponse = new SendSmsResponse();
        try {
            sendSmsResponse = smsCoreService.sendCommonSMS(phoneNumber, tplNumber, attributes);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return sendSmsResponse;

    }

    @Override
    public SendSmsResponse sendSync(String phoneNumber, String tplNumber, HashMap<String,String> attributes) {
        SendSmsResponse sendSmsResponse = new SendSmsResponse();
        try {
            sendSmsResponse = smsCoreService.sendCommonSMS(phoneNumber, tplNumber, attributes);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return sendSmsResponse;
    }

    @Override
    public SendSmsResponse send(String phoneNumber, String tplNumber, HashMap<String,String> attributes, String verifyCode) {
        SendSmsResponse sendSmsResponse = new SendSmsResponse();
        VerifyCodeVO verify = new VerifyCodeVO();
        verify.setCode(verifyCode);
        VerifyCodeVO codeVo = cacheClient.get("sms", phoneNumber, VerifyCodeVO.class);
        if(codeVo != null){
            verify.setTimes(codeVo.getTimes()+1);
            if(verify.getTimes() > 20){
                throw new SmsSendTooFastException();
            }
        }else{
            verify.setTimes(1);
        }
        cacheClient.set("sms", phoneNumber, verify, 10 * 60000);
        try {
            sendSmsResponse = smsCoreService.sendCommonSMS(phoneNumber, tplNumber, attributes);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return sendSmsResponse;
    }

    @Override
    public Boolean check(String phone, String verifyCode) {
        if(mazConfig.getSmsDebug() && verifyCode.equals(mazConfig.getSmsDebugCode())){
            return true;
        }
        VerifyCodeVO codeVo = cacheClient.get("sms", phone, VerifyCodeVO.class);
        if(codeVo == null || !codeVo.getCode().equals(verifyCode)){
            throw new IncorrectSmsCodeException();
        }
        return true;
    }
}
